package com.gin.ehdownloader.interceptor;

import lombok.extern.slf4j.Slf4j;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;

/**
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2022/10/10 14:34
 **/
@Slf4j
public class LoggingInterceptor implements Interceptor {
    @NotNull
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();

        long startTime = System.currentTimeMillis();
        log.info("发送请求: {}",request.url());
        Response response = chain.proceed(request);
        long endTime = System.currentTimeMillis();
        log.info("请求成功: {} code:{} 耗时: {} ms",request.url(),response.code(),(endTime - startTime));
        return response;
    }
}
